/**
 * Created by meiguizhinian on 2018/6/27
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//单例/全静态/枚举
ivy.SoundManager = {
	getInstance : function() {
		return this;
	},

	initWithFile : function (filename) {
		let is = new ivy.InputStream(filename);
		cc.assert(!is.empty(), "读取音乐文件失败"+filename);
		let fileCount = is.readJInt16();
		this.soundNameVec_ = [];
		for (let i = 0; i < fileCount; ++i) {
			this.soundNameVec_[i] = "sounds/" + is.readStringUTF8();
		}
	},

	//编辑器关联
	preloadAllEditorSound : function() {
		for (let i=0; i!==this.soundNameVec_.length; ++i) {
			this.preloadSound(this.soundNameVec_[i]);
		}
	},
	unloadAllEffectFromEditor : function() {
		this.setAllSoundStop();
		for (let i=0; i!==this.soundNameVec_.length; ++i) {
			this.unloadEffect(this.soundNameVec_[i]);
		}
	},

	/*
	 * playSound(index, loop = false)
	 */
	playSound : function(filenameOrIndex, loop=false) {
		if (!this.isSoundCanPlay_) {
			return;
		}
		let filename = null;
		if (Object.prototype.toString.call(filenameOrIndex)==="[object String]") {
			filename = filenameOrIndex;
		} else {
			if (filenameOrIndex<0 || filenameOrIndex>=this.soundNameVec_.length) {
				return;
			}
			filename = this.soundNameVec_[filenameOrIndex];
		}

		if (this.isOpenOptimize_) {
			if (this._isConformToAudioCountPerFrame(filename)) {
				cc.audioEngine.playEffect(filename, loop)
			}
		} else {
			cc.audioEngine.playEffect(filename, loop)
		}
	},
	//
	preloadSound : function(filename) {
		if (filename && filename.length>0) {
			if (cc.sys.isNative) {
				jsb.AudioEngine.preload(filename)
			}
		}
	},
	unloadEffect : function(filename) {
		if (filename && filename.length>0) {
			cc.audioEngine.unloadEffect(filename)
		}
	},

	setAllSoundStop : function() {
		cc.audioEngine.stopMusic(true);
	},

	preloadMusic : function(filename) {
		if (!filename || filename.length===0) {
			return;
		}
		if (cc.sys.isNative) {
			jsb.AudioEngine.preload(filename);
		}
	},
	playMusic : function(filename, loop=true) {
		if (!filename || filename.length===0) {
			return;
		}

		this.currentPlayingMusicName_ = filename;
		this.currentPlayingMusicLoop_ = loop;
		cc.audioEngine.stopMusic(true);
		if (this.isMusicCanPlay_) {
			cc.audioEngine.playMusic(filename, loop);
		}
	},
	setMusicStop : function() {
		cc.audioEngine.stopMusic(false);
	},
	setMusicPause : function() {
		if (this.isMusicCanPlay_) {
			cc.audioEngine.setMusicVolume(0);
			cc.audioEngine.pauseMusic();
		}
	},
	setMusicResume : function() {
		if (this.isMusicCanPlay_) {
			cc.audioEngine.setMusicVolume(this.musicVolume_);
			cc.audioEngine.resumeMusic();
		}
	},

	isMusicCanPlay : function() {
		return this.isMusicCanPlay_;
	},
	isSoundCanPlay : function() {
		return this.isSoundCanPlay_;
	},
	setMusicCanPlay : function(canPlay) {
		if (this.isMusicCanPlay_ !== canPlay) {
			this.isMusicCanPlay_ = canPlay;
			if (canPlay) {
				cc.audioEngine.setMusicVolume(this.musicVolume_);
				cc.audioEngine.resumeMusic();
			} else {
				cc.audioEngine.setMusicVolume(0);
				cc.audioEngine.pauseMusic();
			}
		}
	},
	setSoundCanPlay : function(canPlay) {
		this.isSoundCanPlay_ = canPlay;
	},

	getMusicVolume : function() {
		return this.musicVolume_;
	},

	setSoundVolume : function(v) {
		if (this.isSoundCanPlay_) {
			this.soundVolume_ = v;
		}
	},
	setMusicVolume : function(v) {
		if (this.isMusicCanPlay_) {
			this.musicVolume_ = v;
			cc.audioEngine.setMusicVolume(v)
		}
	},

	pauseAll : function() {
		cc.audioEngine.pauseMusic();
		cc.audioEngine.pauseAllEffects();
	},
	resumeAll : function() {
		cc.audioEngine.resumeAllEffects();
		cc.audioEngine.resumeMusic();
	},

	//开启优化-------存在遐思，谨慎使用
	setMaxAudioPerFrame : function(count) {
		this.maxAudioPerFrame_ = count;
	},
	enableOptimize : function(enable) {
		this.isOpenOptimize_ = enable;
	},

	update : function(dt) {
		if (this.soundNameVec_.length!==0) {
			this.soundsMap_.clear();
		}
	},
	getCurMusic : function() {
		return this.currentPlayingMusicName_;
	},
	playLastMusic : function() {
		this.playMusic(this.currentPlayingMusicName_, this.currentPlayingMusicLoop_, this.currentPlayingMusicVloume_, this.currentPlayingMusicProfile_);
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//是否满足统一时刻播发统一音效最大数量
	_isConformToAudioCountPerFrame : function(filename) {
		let ret = true;
		if (this.soundsMap_.has(filename)) {
			if (this.soundsMap_.get(filename) < this.maxAudioPerFrame_) {
				this.soundsMap_.set(filename, this.soundsMap_.get(filename)+1);
			} else {
				ret = false;
			}
		} else {
			this.soundsMap_.set(filename, 1);
		}
		return ret;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//编辑器相关
	soundNameVec_ : [],
	isSoundCanPlay_ : true,
	isMusicCanPlay_ : true,
	musicVolume_ : 0.3,
	soundVolume_ : 0.3,
	musicAudioId_ : 0,
	soundsMap_ : new LLDictionary(),
	maxAudioPerFrame_ : 5,
	isOpenOptimize_ : false,
	currentPlayingMusicName_ : "",
	currentPlayingMusicLoop_ : true,
	currentPlayingMusicVloume_ : 1.0,
	currentPlayingMusicProfile_ : null,
};
